function ajax({ url, data={}, method="get", beforeSend, timeout=1000, dataType="json", success, error } = {}){
    beforeSend && beforeSend();
    let s = "";
    for(let i in data){
        s += `${i}=${data[i]}&`;
    }
    if(method === "get"){
        url += "?" + s + "__t__=" + Date.now();
    }
    const xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.timeout = timeout;
    if(method === "get"){
        xhr.send();
    }else if(method === "post"){
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(s.slice(0, -1));
    }
    
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            let d = dataType==="json" ? JSON.parse(xhr.responseText) : xhr.responseText;
            success( d );
        }
        if(xhr.readyState === 4 && xhr.status !== 200){
            error && error( xhr.status );
        }
    }
}